#include<bits/stdc++.h>
using namespace std;
int t,n,a[200005],ans,m[200005],mp[1000005];
long long dp[200005];
void s(){
    cin>>n;
    memset(mp,0,sizeof(mp));
    for(int i=1;i<=n;i++){
        cin>>a[i];
        m[i]=mp[a[i]];
        mp[a[i]]=i;
    }
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        if(!m[i]){
            dp[i]=dp[i-1];
            continue;
        }
        long long sum=a[i];
        if(m[m[i]+1]==m[i]) sum+=dp[m[i]];
        else sum+=dp[m[i]+1];
        for(int j=m[i]+2;j<i;j++){
            if(m[j]==j-1) sum+=a[j];
        }
        dp[i]=max(dp[i-1],sum);
    }
    cout<<dp[n]<<endl;
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>t;
    while(t--) s();
    return 0;
}
